package com.youlai.boot.modules.warehouse.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.warehouse.model.WarehouseForm;
import com.youlai.boot.modules.warehouse.model.WarehouseVO;
import com.youlai.boot.modules.warehouse.service.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.modules.warehouse.model.ProductWarehousePageQuery;
@Tag(name = "仓储公司接口")
@RestController
@RequestMapping("/api/v1/warehouse")
@RequiredArgsConstructor
public class WarehouseController {

    private final WarehouseService warehouseService;

    @Operation(summary = "仓储公司分页列表")
    @GetMapping("/page")
    public PageResult<WarehouseVO> listWarehouses(
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<WarehouseVO> result = warehouseService.getWarehousePage(name, pageNum, pageSize);
        return PageResult.success(result);
    }

    @Operation(summary = "仓储公司详情")
    @GetMapping("/{id}")
    public Result<WarehouseVO> getWarehouse(@PathVariable Long id) {
        WarehouseVO warehouse = warehouseService.getWarehouseById(id);
        return Result.success(warehouse);
    }

    @Operation(summary = "添加仓储公司")
    @PostMapping
    public Result<Long> addWarehouse(@Valid @RequestBody WarehouseForm warehouse) {
        Long id = warehouseService.saveWarehouse(warehouse);
        return Result.success(id);
    }

    @Operation(summary = "修改仓储公司")
    @PutMapping("/{id}")
    public Result<Boolean> updateWarehouse(
            @PathVariable Long id,
            @Valid @RequestBody WarehouseForm warehouse) {
        warehouse.setId(id);
        warehouseService.updateWarehouse(warehouse);
        return Result.success(true);
    }

    @Operation(summary = "删除仓储公司")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteWarehouse(@PathVariable Long id) {
        boolean result = warehouseService.deleteWarehouse(id);
        return Result.judge(result);
    }

    @Operation(summary = "获取商品仓储公司列表")
    @GetMapping("/product/{productId}/page")
    public PageResult<WarehouseVO> getProductWarehouseList(@PathVariable Long productId, ProductWarehousePageQuery query) {
        Page<WarehouseVO> warehouseList = warehouseService.getProductWarehousePage(productId, query);
        return PageResult.success(warehouseList);
    }

    @Operation(summary = "添加商品仓储公司")
    @PostMapping("/product/{productId}/warehouse/{warehouseId}")
    public Result<Boolean> addProductWarehouse(@PathVariable Long productId, @PathVariable Long warehouseId) {
        boolean result = warehouseService.saveProductWarehouse(productId, warehouseId);
        return Result.judge(result);
    }
    
    @Operation(summary = "删除商品仓储公司")
    @DeleteMapping("/product/{productId}/warehouse/{warehouseId}")
    public Result<Boolean> deleteProductWarehouse(@PathVariable Long productId, @PathVariable Long warehouseId) {
        boolean result = warehouseService.deleteProductWarehouse(productId, warehouseId);
        return Result.judge(result);
    }
}